package com.gaowl.yygh.hosp.controller.api;


import com.gaowl.yygh.commom.exception.HospitalException;
import com.gaowl.yygh.commom.result.Result;
import com.gaowl.yygh.commom.result.ResultCodeEnum;
import com.gaowl.yygh.commom.utils.MD5;
import com.gaowl.yygh.common.helper.HttpRequestHelper;
import com.gaowl.yygh.hosp.service.DepartmentService;
import com.gaowl.yygh.hosp.service.HospitalService;
import com.gaowl.yygh.hosp.service.HospitalSetService;
import com.gaowl.yygh.hosp.service.ScheduleService;
import com.gaowl.yygh.model.hosp.Department;
import com.gaowl.yygh.model.hosp.Hospital;
import com.gaowl.yygh.model.hosp.Schedule;
import com.gaowl.yygh.vo.hosp.DepartmentQueryVo;
import com.gaowl.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;  // 此处声明的为接口，而非其实现类

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 接口1：上传医院
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);   // 将String[]转换为Object类型，便于后续处理

        // 1.校验签名
        verifySignature(request);

        // 2.图片经过了base64编码，传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        // 3.调用service方法，存储医院信息
        hospitalService.save(paramMap);
        return Result.ok();
    }

    // 接口2：查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        // 获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.校验签名
        verifySignature(request);

        // 2.调用service方法，根据医院编号查询医院信息
        String hoscode = (String)paramMap.get("hoscode");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    // 接口3：上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.检验签名
        verifySignature(request);

        // 2.调用service的方法，保存科室信息
        departmentService.save(paramMap);
        return Result.ok();
    }

    // 接口4：查询科室
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.校验签名
        verifySignature(request);

        // 2.设置当前页 和 每页记录数；并将hoscode封装为对象
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        String hoscode = (String)paramMap.get("hoscode");
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        // 3.调用service方法，进行查询
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    // 接口5：删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取传递过来的信息，拿到 医院编号 和 科室编号
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        verifySignature(request);

        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    // 接口6：上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.校验签名
        verifySignature(request);

        // 2.调用service中的方法，保存排班信息
        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 接口7：查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.校验签名
        verifySignature(request);

        // 2.设置当前页 和 每页记录数，并封装 医院编号和科室编号
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        // 调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    // 接口8：删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.校验签名
        verifySignature(request);

        // 2.调用service中的方法，根据医院编号和排班编号，移除排班信息
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }


    // 校验签名
    public void verifySignature(HttpServletRequest request){
        // 获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 1.获取医院hoscode
        String hoscode = (String)paramMap.get("hoscode");
        String hospSign = (String)paramMap.get("sign");
        // 2.根据传递过来医院编码，查询数据库，获得签名并MD5加密
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        // 3.判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
    }

}
